using Elvex;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HUDComponent : MonoBehaviour
{
    [SerializeField]
    private GameObject m_prefab;

    [SerializeField]
    private Vector3 m_Offset;

    public Canvas HUDCanvas;

    public GameObject HUDObject { get; private set; }

    private void Awake()
    {
        HUDObject = Instantiate(m_prefab, HUDCanvas.transform);
        HUDObject.gameObject.SetActive(true);
    }

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if(HUDCanvas.worldCamera && HUDObject && HUDCanvas)
        {
            Vector3 screenpos = Camera.main.WorldToScreenPoint(transform.position + m_Offset);
            RectTransformUtility.ScreenPointToLocalPointInRectangle(
                HUDCanvas.transform as RectTransform,
                screenpos,
                HUDCanvas.worldCamera,
                out var localpos);
            HUDObject.transform.localPosition = localpos;
        }
    }

    private void OnDestroy()
    {
        Destroy(HUDObject);
    }
}
